/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.ladon.apm.common.enums;

import org.apache.commons.lang3.StringUtils;

/**
 * 描述:  地区本地IP段枚举
 *
 * @author tianyuliang
 * @version $Id: SiteLocalIpEnum.java, v0.1 2019/3/27
 */
public enum SiteLocalIpEnum implements EnumInterface {

    /**
     * 本地回环地址 Local类 127.0.0.0 - 127.255.255.255
     */
    L("L", "127.0.0.0", "127.255.255.255"),

    /**
     * 私有IP地址 A类 10.0.0.0 - 10.255.255.255 总共 1658万个
     */
    A("A", "10.0.0.0", "10.255.255.255"),

    /**
     * 私有IP地址 B类 172.16.0.0 - 172.31.255.255  总共 97.5万个
     */
    B("B", "172.16.0.0", "172.31.255.255"),

    /**
     * 私有IP地址 C类 192.168.0.0 - 192.168.255.255  总共 6.5万个
     */
    C("C", "192.168.0.0", "192.168.255.255");

    private String start;

    private String end;

    private String type;

    SiteLocalIpEnum(String type, String start, String end) {
        this.type = type;
        this.start = start;
        this.end = end;
    }

    public static SiteLocalIpEnum of(String ip) {
        if (StringUtils.isBlank(ip) || !ip.contains(".")) {
            return null;
        }

        String[] values = ip.split(".");
        if (values == null || values.length != 4) {
            return null;
        }

        String first = values[0];
        for (SiteLocalIpEnum ipEnum : SiteLocalIpEnum.values()) {
            String[] localValues = ipEnum.start.split(".");
            if (localValues[0].equals(first)) {
                return ipEnum;
            }
        }
        return null;
    }

    public String getStart() {
        return start;
    }

    public String getEnd() {
        return end;
    }

    @Override
    public String getCode() {
        return start;
    }

    @Override
    public String getDesc() {
        return type;
    }

}
